package com.taijihuabao.modules.service.impl;

import com.alibaba.excel.util.CollectionUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.taijihuabao.common.exception.ServiceException;
import com.taijihuabao.common.jsonresult.ApplicationResponseCode;
import com.taijihuabao.modules.entity.DeptRoleEntity;
import com.taijihuabao.modules.entity.DeptUserEntity;
import com.taijihuabao.modules.mapper.DeptRoleMapper;
import com.taijihuabao.modules.mapper.DeptUserMapper;
import com.taijihuabao.modules.mapper.UserMapper;
import com.taijihuabao.modules.model.dept.user.DeptUserDTO;
import com.taijihuabao.modules.model.dept.user.DeptUserPageDTO;
import com.taijihuabao.modules.model.user.vo.UserInfoVO;
import com.taijihuabao.modules.service.DeptUserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * 部门用户管理
 * @author xiao
 */
@Service
public class DeptUserServiceImpl implements DeptUserService {

    @Resource
    private DeptUserMapper deptUserMapper;

    @Resource
    private UserMapper userMapper;

    @Resource
    private DeptRoleMapper deptRoleMapper;

    @Override
    public List<DeptUserEntity> selectDepts() {
        List<DeptUserEntity> list = deptUserMapper.selectDepts();
        if(CollectionUtils.isEmpty(list)){
            throw new ServiceException(ApplicationResponseCode.DEPT_USER_NOTFOUND);
        }
        return list;
    }

    @Override
    public PageInfo selectAllDept(DeptUserPageDTO deptUserDTO) {
        List<DeptUserEntity> list = deptUserMapper.selectAllDept(deptUserDTO);
        if(CollectionUtils.isEmpty(list)){
            throw new ServiceException(ApplicationResponseCode.DEPT_USER_NOTFOUND);
        }
        PageHelper.startPage(deptUserDTO.getPage(), deptUserDTO.getSize());
        PageInfo pageInfo = new PageInfo(list);
        return pageInfo;
    }

    @Override
    public List<DeptUserEntity> selectDeptByDeptCode(String deptCode) {
        List<DeptUserEntity> list = deptUserMapper.selectDeptByDeptCode(deptCode);
        if(CollectionUtils.isEmpty(list)){
            throw new ServiceException(ApplicationResponseCode.DEPT_USER_NOTFOUND);
        }
        return list;
    }

    @Override
    public List<DeptUserEntity> selectDeptByUserId(Long userId) {
        List<DeptUserEntity> list = deptUserMapper.selectDeptByUserId(userId);
        if(CollectionUtils.isEmpty(list)){
            throw new ServiceException(ApplicationResponseCode.DEPT_USER_NOTFOUND);
        }
        return list;
    }

    @Override
    public List<DeptUserEntity> selectDeptByName(String name) {
        List<DeptUserEntity> list = deptUserMapper.selectDeptByName(name);
        if(CollectionUtils.isEmpty(list)){
            throw new ServiceException(ApplicationResponseCode.DEPT_USER_NOTFOUND);
        }
        return list;
    }

    @Override
    public DeptUserEntity selectDeptById(Long id) {
        DeptUserEntity deptUserEntity = deptUserMapper.selectDeptById(id);
        if (Objects.isNull(deptUserEntity)){
            throw new ServiceException(ApplicationResponseCode.DEPT_USER_NOTFOUND);
        }
        return deptUserEntity;
    }

    @Override
    public List<DeptUserEntity> selectDeptByDeptRole(String deptRole) {
        List<DeptUserEntity> list = deptUserMapper.selectDeptByDeptRole(deptRole);
        if(CollectionUtils.isEmpty(list)){
            throw new ServiceException(ApplicationResponseCode.DEPT_USER_NOTFOUND);
        }
        return list;
    }

    @Override
    public int insertDept(DeptUserDTO deptUserEntity) {
        UserInfoVO userInfoVO = userMapper.selectById(Long.valueOf(deptUserEntity.getUserId()));
        //判断用户是否存在
        if(Objects.isNull(userInfoVO)){
            throw new ServiceException(ApplicationResponseCode.USER_NOT_EXIST);
        }
        //一个用户可以多个角色
        int result = 0;
        if(!CollectionUtils.isEmpty(deptUserEntity.getDeptRoleList())){
            for(String deptRole : deptUserEntity.getDeptRoleList()){
                deptUserEntity.setDeptRole(deptRole);
                deptUserEntity.setId(null);
                //判断角色是否存在
                DeptRoleEntity d1 = deptRoleMapper.selectDeptByCodeAndDeptCode(
                        deptUserEntity.getDeptCode(),deptRole);
                if(!Objects.isNull(d1)){
                    throw new ServiceException(ApplicationResponseCode.DEPTROLEID_HAS_EXIST);
                }
                //判断部门用户信息id是否唯一
                DeptUserEntity d2 = deptUserMapper.selectDeptById(deptUserEntity.getId());
                if(!Objects.isNull(d2)){
                    throw new ServiceException(ApplicationResponseCode.DEPT_HAS_EXIST);
                }
                result = deptUserMapper.insertDept(deptUserEntity);
                if(result<1){
                    throw new ServiceException(ApplicationResponseCode.DEPT_INSERT_FAILED);
                }
            }
        }
        return result;
    }

    @Override
    public int updateDept(DeptUserDTO deptUserEntity) {
        //判断用户是否存在
        List<DeptUserEntity> list = deptUserMapper.selectDeptByUserId(deptUserEntity.getUserId());
        if(list.isEmpty()){
            throw new ServiceException(ApplicationResponseCode.DEPT_USER_NOTFOUND);
        }
        //判断角色是否存在
        DeptRoleEntity d1 = deptRoleMapper.selectDeptByCodeAndDeptCode(
                deptUserEntity.getDeptRole(),deptUserEntity.getDeptCode());
        if(Objects.isNull(d1)){
            throw new ServiceException(ApplicationResponseCode.DEPTROLEID_NOT_EXIST);
        }
        int result = deptUserMapper.updateDept(deptUserEntity);
        if(result<1){
            throw new ServiceException(ApplicationResponseCode.DEPT_UPDATE_FAILED);
        }
        return result;
    }

    @Override
    public Integer deleteDept(DeptUserDTO deptUserEntity) {
        //判断部门用户id是否存在
        DeptUserEntity d1 = deptUserMapper.selectDeptById(deptUserEntity.getId());
        if(Objects.isNull(d1)){
            throw new ServiceException(ApplicationResponseCode.DEPT_NOT_EXIST);
        }
        int result = deptUserMapper.deleteDept(deptUserEntity);
        if(result<1){
            throw new ServiceException(ApplicationResponseCode.DEPT_DELETE_FAILED);
        }
        return result;
    }
}
